package com.apes.auto.car.service;

import com.apes.auto.car.model.CarPartElectronicCatalogue;
import com.apes.auto.car.model.PartsElectronicCatalogue;
import com.apes.auto.car.model.PartsIdClass;
import com.apes.auto.car.repository.CarPartElectronicCatalogueRepository;
import com.apes.auto.car.repository.PartsElectronicCatalogueRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CarPartElectronicCatalogueService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/8/2 18:01
 * @Version 1.0
 **/
@Service("carPartElectronicCatalogueService")
public class CarPartElectronicCatalogueService extends DomainService {
    @Autowired
    private CarPartElectronicCatalogueRepository catalogueRepository;

    @Autowired
    private PartsElectronicCatalogueRepository partsPepository;


    public CarPartElectronicCatalogue save(SimpleRequest request) {
        CarPartElectronicCatalogue o = request.getO(CarPartElectronicCatalogue.class);
        o.setLrr(this.getPerson(request));
        o.setLrrq_sj(new Date());
        o.setTs01(DateUtil.format(o.getLrrq_sj(), "yyyyMMdd.hhmmss.SSS"));
        CarPartElectronicCatalogue catalogue = catalogueRepository.saveAndFlush(o);
        return catalogue;
    }

    public String delete(SimpleRequest request) {
        CarPartElectronicCatalogue o = request.getO(CarPartElectronicCatalogue.class);
        PartsIdClass partsIdClass = new PartsIdClass(o.getPjmcid(), o.getPjmc());
        catalogueRepository.delete(partsIdClass);
        return "删除成功";
    }

    /**
     * 生成数据中心电子目录
     *
     * @param map
     * @return
     */
    public void batchSave(Map<String, String> map) {
        List<PartsElectronicCatalogue> partsAll = partsPepository.findAll(JpaDsl.toCriteriaByEq());
        List<CarPartElectronicCatalogue> catalogues = new ArrayList<>();
        for (int i = 0; i < partsAll.size(); i++) {
            if ("3434".equals(partsAll.get(i).getZDYID()) && "无".equals(map.get("FDJQTJS"))) {
                continue;
            }
            CarPartElectronicCatalogue o = new CarPartElectronicCatalogue();
            o.setPzcxid(map.get("PZCXID"));
            o.setOeid(map.get("OEID"));
            o.setLrrq_sj(new Date());
            o.setTs01(DateUtil.format(o.getLrrq_sj(), "yyyyMMdd.hhmmss.SSS"));
            o.setLrr(map.get("LRR"));
            o.setPlid(partsAll.get(i).getPLID());
            o.setPlmc(partsAll.get(i).getPLMC());
            o.setPjmcid(partsAll.get(i).getPJMCID());
            o.setPjmc(partsAll.get(i).getPJMC());
            catalogues.add(o);
        }
        catalogueRepository.save(catalogues);
    }
}
